package utils

import (
	"net/url"
	"regexp"
)

var urlRegex = regexp.MustCompile(`^(http|https)://[a-zA-Z0-9-.]+\.[a-zA-Z]{2,}(:[0-9]{2,5})?(/[a-zA-Z0-9#_\-./?%=&]*)*$`)
var ipv4Regx = regexp.MustCompile(`^(http|https)://([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-.]+)\:?([0-9]{2,5})?(\/[a-zA-Z0-9#_\-./?%=&]*)*$`)

func IsValidAPI(apiStr string) bool {
	if !urlRegex.MatchString(apiStr) {
		if !ipv4Regx.MatchString(apiStr) {
			return false
		}
	}
	u, err := url.ParseRequestURI(apiStr)
	return err == nil && u.Host != ""
}
